package org.hhchat.others.q360;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created this one by HMH on 2017/8/26.
 */
public class Main {

    public static void main(String[] strs) {
        Scanner scanner = new Scanner(System.in);
        int n,m;
        n = scanner.nextInt();
        m = scanner.nextInt();
        int[] a = new int[n];
        int[] b = new int[m];
        while (n-- > 0) {
            
        }
        Swap2Balance(a, b);
        System.out.println(Arrays.toString(a));
        System.out.println(bestDiff);
    }

    static int count=0;
    static int bestDiff=Integer.MAX_VALUE;
    /*
     * var a=[100,99,98,1,2, 3]; var b=[1, 2, 3, 4,5,40];
     * 假设序列a，b中元素的和为sum_a和sum_b。假设aa和bb分别为序列a，b中的元素，
     * 则交换aa，bb后序列的和变为sum_a-aa+bb，sum_b+aa-bb；两序列的差为（sum_a-aa+bb）-（sum_b+aa-bb）=
     * sum_a-sum_b-2*（aa-bb）；
     * 所以可以扫描序列a，b中的元素，找到使abs（sum_a-sum_b-2*（aa-bb））最小的两个元素进行交换，重复此过程，
     * 直至两序列的差无法减小。
     */
    public static void Swap2Balance(int[] pa, int[] pb) {
        int suma = 0, sumb = 0;
        for (int i = 0; i < pa.length; i++) {
            suma += pa[i];
        }
        for(int i=0;i<pb.length;i++) {
            sumb += pb[i];
        }
        int diff = suma - sumb;
        while (diff != 0) {
            int besti = 0, bestj = 0;
            int bestChange = 0;
            for (int i = 0; i < pa.length; ++i) {
                for (int j = 0; j < pb.length; ++j) {
                    int change = pa[i] - pb[j];
                    // 交换后差为(suma-pa[i]+pb[j])-(sumb+pa[i]-pb[j])=diff-2*change
                    if (Math.abs(diff - 2 * change) < Math.abs(diff - 2 * bestChange)) {
                        besti = i;
                        bestj = j;
                        bestChange = change;
                    }
                }
            }

            int temp = pa[besti];
            pa[besti] = pb[bestj];
            pb[bestj] = temp;
            suma = suma - bestChange;
            sumb = sumb + bestChange;
            diff = suma - sumb;
            bestDiff = Integer.min(bestDiff, diff);
            if (++count>=2)// 差不能再缩小
                return;
        }
    }
}
